package year2022.redis;

import org.apache.storm.shade.org.apache.commons.lang.StringUtils;

import java.io.*;
import java.net.Socket;

public class RedisClient {
    private static String CRLF="\r\n";

    public static void main(String[] args) throws Exception {
        client();
    }

    private static void client() {
        Socket socket = null;
        try {
            socket = new Socket("127.0.0.1", 6379);
            OutputStream output = socket.getOutputStream();
            InputStream input = socket.getInputStream();

            String auth = buildRdsTcp(new String[]{"AUTH", "caijin_dev"});
            request(output, auth);
            System.out.println("auth recv：\n" + response(input));

            String set = buildRdsTcp(new String[]{"set", "key1", "666"});
            request(output, set);
            System.out.println("set recv：\n" + response(input));

            String get = buildRdsTcp(new String[]{"get", "key1"});
            request(output, get);
            System.out.println("get recv：\n" + response(input));
        } catch (IOException e){
            e.printStackTrace();
        } finally {
            try {
                socket.shutdownOutput();
                socket.shutdownInput();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static String buildRdsTcp(String[] valArr){
        if (valArr==null || valArr.length==0){
            return "";
        }

        StringBuffer strBuild = new StringBuffer();
        strBuild.append("*").append(valArr.length).append(CRLF);

        for (String val : valArr){
            buildRdsTcp(strBuild, val);
        }

        return strBuild.toString();
    }

    public static StringBuffer buildRdsTcp(StringBuffer strBuild, String val){
        if (StringUtils.isBlank(val)){
            return strBuild;
        }

        strBuild.append("$").append(val.length()).append(CRLF).append(val).append(CRLF);

        return strBuild;
    }

    public static void request(OutputStream output, String content) throws IOException {
        output.write(content.getBytes());
        output.flush();
    }

    public static String response(InputStream input) throws IOException {
        StringBuffer strBuild = new StringBuffer();

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(input, "UTF-8"));

        int line = 1;
        int currLine = 0;

        while (currLine<line){
            currLine++;
            String str = bufferedReader.readLine();
            if (currLine==1 && str.startsWith("*")){
                line = Integer.parseInt(str.substring(1)) + 1;
            }

            if (str.startsWith("$")){
                strBuild.append(str).append(CRLF);
                str = bufferedReader.readLine();
                strBuild.append(str).append(CRLF);
            } else {
                strBuild.append(str).append(CRLF);
            }
        }

        return strBuild.toString();
    }

}
